package com.xm.xmapp;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.RequestInterFaceCode;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Apply_refund_conditionRequest;
import com.xm.xmapp.request.bean.Get_user_money_infoRequest;
import com.xm.xmapp.response.bean.ResponseApplyRefundCondition;
import com.xm.xmapp.response.bean.ResponseUserMoney;
import com.xm.xmapp.tools.StringTools;

public class ReturnMoneyActivity extends BaseActivity implements OnClickListener {
	private TextView tv_info;
	private LinearLayout ll_car_frozen, ll_frozen;
	private TextView tv_car_frozen, tv_frozen;
	private ImageView iv_car_frozen, iv_frozen;
	/**
	 * RequestInterFaceCode.Apply_refund_money中类型对应
	 */
	private int flag;
	private ResponseUserMoney bean;

	/**
	 * 0退还押金后调用
	 */
	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case Type.A_ReturnMoney.FINISH:
			finish();
			break;

		default:
			break;
		}

		return super.dataChang(flag, data);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_returnmoney);
	}

	@Override
	public void initView() {
		tv_info = (TextView) findViewById(R.id.tv_info);
		ll_car_frozen = (LinearLayout) findViewById(R.id.ll_car_frozen);
		ll_frozen = (LinearLayout) findViewById(R.id.ll_frozen);
		tv_car_frozen = (TextView) findViewById(R.id.tv_car_frozen);
		tv_frozen = (TextView) findViewById(R.id.tv_frozen);
		iv_car_frozen = (ImageView) findViewById(R.id.iv_car_frozen);
		iv_frozen = (ImageView) findViewById(R.id.iv_frozen);

		findViewById(R.id.tv_next).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startNext();
			}
		});
	}

	/**
	 * 下一步
	 */
	protected void startNext() {
		Log.d("LDW", "Onclick()");
		if (bean != null) {
			final Apply_refund_conditionRequest requestBean = new Apply_refund_conditionRequest();
			requestBean.cash_type = flag;
			if (flag == RequestInterFaceCode.Apply_refund_money.CAR) {
				requestBean.cash_amount = bean.car_frozen_money;
			} else {
				requestBean.cash_amount = bean.frozen_money;
			}
			promptDialog.show();
			MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

				@Override
				public void onResponse(String response) {

					promptDialog.dismiss();
					Log.d("LDW", "response==>" + response);
					ResponseApplyRefundCondition base = JSON.parseObject(response, ResponseApplyRefundCondition.class);
					intent = new Intent(MyAppcation.getMyAppcation(), SumbitBankInfoActivity.class);
					intent.putExtra("cash_type", requestBean.cash_type.toString());
					double cash_amount = Double.parseDouble(requestBean.cash_amount.toString());
					intent.putExtra("cash_amount", StringTools.getString(cash_amount, "0.00"));
					intent.putExtra("bean", base);
					startActivity(intent);
				}
			}, new DisssmissInterFace() {

				@Override
				public void putError(VolleyError error) {
					promptDialog.dismiss();
					Log.d("LDW", "response==>error");
				}
			});
		} else {
			Log.d("LDW", "get_user_money_infoRequest()");
			get_user_money_infoRequest();
		}

	}

	@Override
	public void initData() {

		setMyTitle("押金退款");
		flag = RequestInterFaceCode.Apply_refund_money.CAR;
		bean = (ResponseUserMoney) getIntent().getSerializableExtra("bean");
		if (bean != null) {
			tv_frozen.setText(StringTools.getString(bean.frozen_money, "0.00") + "元");
			tv_car_frozen.setText(StringTools.getString(bean.car_frozen_money, "0.00") + "元");
			if (bean.car_frozen_money <= 0) {
				flag = RequestInterFaceCode.Apply_refund_money.WZ;
				ll_car_frozen.setVisibility(View.GONE);
			} else {
				flag = RequestInterFaceCode.Apply_refund_money.CAR;
				ll_car_frozen.setVisibility(View.VISIBLE);
			}

			if (bean.frozen_money <= 0) {
				flag = RequestInterFaceCode.Apply_refund_money.CAR;
				ll_frozen.setVisibility(View.GONE);
			} else {
				flag = RequestInterFaceCode.Apply_refund_money.WZ;
				ll_frozen.setVisibility(View.VISIBLE);
			}
			if (bean.refund_content != null && !bean.refund_content.equals("")) {
				tv_info.setText(bean.refund_content);
			}

		} else {
			get_user_money_infoRequest();
		}
		setImg();
	}

	private void get_user_money_infoRequest() {

		Get_user_money_infoRequest requestBean = new Get_user_money_infoRequest();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				bean = JSON.parseObject(response, ResponseUserMoney.class);
				tv_frozen.setText(StringTools.getString(bean.frozen_money, "0.00") + "元");
				tv_car_frozen.setText(StringTools.getString(bean.car_frozen_money, "0.00") + "元");
				if (bean.car_frozen_money <= 0) {
					flag = RequestInterFaceCode.Apply_refund_money.WZ;
					ll_car_frozen.setVisibility(View.GONE);
				} else {
					flag = RequestInterFaceCode.Apply_refund_money.CAR;
					ll_car_frozen.setVisibility(View.VISIBLE);
				}

				if (bean.frozen_money <= 0) {
					flag = RequestInterFaceCode.Apply_refund_money.CAR;
					ll_frozen.setVisibility(View.GONE);
				} else {
					flag = RequestInterFaceCode.Apply_refund_money.WZ;
					ll_frozen.setVisibility(View.VISIBLE);
				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	private void setImg() {

		if (flag == RequestInterFaceCode.Apply_refund_money.CAR) {
			iv_car_frozen.setImageResource(R.mipmap.returnchoose);
			iv_frozen.setImageResource(R.mipmap.return_no_choose);
		} else {
			iv_car_frozen.setImageResource(R.mipmap.return_no_choose);
			iv_frozen.setImageResource(R.mipmap.returnchoose);
		}

	}

	@Override
	public void initEvent() {
		ll_car_frozen.setOnClickListener(this);
		ll_frozen.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.ll_car_frozen:
			if (flag != RequestInterFaceCode.Apply_refund_money.CAR) {
				flag = RequestInterFaceCode.Apply_refund_money.CAR;
				setImg();
			}
			break;

		case R.id.ll_frozen:
			if (flag != RequestInterFaceCode.Apply_refund_money.WZ) {
				flag = RequestInterFaceCode.Apply_refund_money.WZ;
				setImg();
			}
			break;

		default:
			break;
		}
	}

}
